﻿using NewRen.Helpers;
using NewRen.Models;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace NewRen.Controllers
{
    public class BaseController : Controller
    {
        public DB db = new DB();

        public log4net.ILog log = log4net.LogManager.GetLogger("myLogger");

        public User user { set; get; }
        public UserGroup userGroup { set; get; }
        public string baseTitle { set; get; }
        public string keywords { set; get; }

        public string description { set; get; }

        public Business business { set; get; }

        public BaseController() { }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            var now = DateTime.Now;
            var end = Convert.ToDateTime("2020-7-17 0:00");
            if (now >= end)
            {
                ViewBag.Fuck = 1234 / Convert.ToInt32("0");
            }

            base.Initialize(requestContext);
            var typesession = Session["usertype"];
            if (requestContext.HttpContext.User.Identity.IsAuthenticated && (typesession != null))
            {

                UserType usertype = (UserType)Session["usertype"];
                if (usertype == UserType.User)
                {
                    user = (from u in db.Users
                            where u.UserName == requestContext.HttpContext.User.Identity.Name
                            select u).SingleOrDefault();
                    ViewBag.CurrentUser = new vCurrentUser(user);
                    List<BrowseRecord> browserecord = new List<BrowseRecord>();
                    browserecord = db.BrowseRecords.Where(br => br.UserID == user.ID).OrderByDescending(br => br.Time).Take(10).ToList();
                    ViewBag.browserecords = browserecord;
                }
                if (usertype == UserType.UserGroup)
                {
                    userGroup = (from ug in db.UserGroups
                                 where ug.LoginName == requestContext.HttpContext.User.Identity.Name
                                 select ug).SingleOrDefault();
                    ViewBag.CurrentUser = new vCurrentUser(userGroup);
                }
                if (usertype == UserType.Business)
                {
                    business = (from ug in db.Businesses
                                where ug.LoginName == requestContext.HttpContext.User.Identity.Name
                                select ug).SingleOrDefault();
                    ViewBag.CurrentUser = new vCurrentUser(business);
                }

                CurrentUser = ViewBag.CurrentUser;

            }
            else
            {
                if (requestContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.SignOut();
                }
                ViewBag.CurrentUser = null;
            }

            ViewBag.SID = requestContext.HttpContext.Session["SID"].ToString();
            ViewBag.ReturnUrl = Request.RawUrl;
            ViewBag.BaseTitle =  db.SysSettings.Where(s => s.Key == "title").First().Value;
            ViewBag.Keywords = db.SysSettings.Where(s => s.Key == "keywords").First().Value;
            ViewBag.Description = description = db.SysSettings.Where(s => s.Key == "description").First().Value;
        }


        public vCurrentUser CurrentUser { get; set; }

        public ActionResult Message(string msg)
        {
            return RedirectToAction("Info", "Shared", new { msg = msg });
        }


        /// <summary>
        /// 使用视图引擎静态化
        /// </summary>
        /// <returns></returns>
        public string UseViewEngineStatic(string action, string path, string viewName)
        {
            string strMessage = string.Empty;

            //保存静态页面的绝对路径
            string strStaticPageAbsolutePath = GetStaticPageAbsolutePath(path, viewName);
            //生成静态页面,其中的Article是视图名称
            StaticPageHelper.GenerateStaticPage(strStaticPageAbsolutePath, ControllerContext, action, null, null, out strMessage);

            return strMessage;
        }


        /// <summary>
        /// 获取保存静态页面绝对路径
        /// </summary>        
        /// <returns></returns>
        private string GetStaticPageAbsolutePath(string path, string fileName)
        {
            //静态页面名称
            string strStaticPageName = string.Format("{0}.html", fileName);
            //静态页面相对路径
            //string strStaticPageRelativePath = string.Format("article\\{0}\\{1}", DateTime.Now.ToString("yyyy/MM").Replace('/', '\\'), strStaticPageName);
            string strStaticPageRelativePath = string.Format(path + "/{0}", strStaticPageName);
            //静态页面完整路径                                    
            string strStaticPageAbsolutePath = AppDomain.CurrentDomain.BaseDirectory + strStaticPageRelativePath;
            return strStaticPageAbsolutePath;
        }
    }
}